//
//  OrderDetailsVC.m
//  代理端
//
//  Created by sliver on 15/12/15.
//  Copyright (c) 2015年 sliver. All rights reserved.
//

#import "OrderDetailsVC.h"
#import "Unit.h"
#import "WXDataService.h"
#import "SBJson.h"
#import "LogisticsVC.h"
#import "OrderDetailModel.h"
#import "OrderNumberView.h"
#import "orderProductView.h"
#import "stationView.h"
#import "wuLiuView.h"
#import "ReturnGoodsVC.h"
@interface OrderDetailsVC ()<wuLiuViewDeleget,orderProductViewDeleget,BackViewDeleget>
{
    UIAlertView *confirmalert;
    UIAlertView *deletealert;
    NSMutableDictionary *dataDic;  //订单
    
    NSMutableArray *dataAry; //物流
}
@property(nonatomic,strong)OrderDetailModel *orderdetailmodel;
@property(nonatomic,strong)NSMutableArray *WuliuArray;
@end

@implementation OrderDetailsVC
-(NSMutableArray *)WuliuArray
{
    if (!_WuliuArray) {
        _WuliuArray = [NSMutableArray array];
    }
    return _WuliuArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dismesscontolller) name:@"quxiao" object:nil];
    dataDic = [[NSMutableDictionary alloc]init];
    dataAry = [[NSMutableArray alloc]init];
    [self loadNav];
    [self loadData];
    // Do any additional setup after loading the view.
}
-(void)loadData{
    [MyMbd addHudToView:self.view];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:_id_ forKey:@"id_"];
    [WXDataService requestWithURL:@"mobile/order/getOrder.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        NSLog(@"%@",result);
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            dataDic = result[@"order"];
            self.orderdetailmodel = [[OrderDetailModel alloc]initWithDataDic:dataDic];
            
            [self postWuLiuNetWorking];
          }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MyMbd hidenHudFromView:self.view];
    }
     ];
    //物流
   
}
-(void)postWuLiuNetWorking
{
    NSMutableDictionary *params2 = [[NSMutableDictionary alloc]init];
    [params2 setObject:_id_ forKey:@"order_id_"];
    [WXDataService requestWithURL:@"mobile/kd/logisticsInformation.jhtml" params:params2 httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        NSLog(@"%@",result);
        [MyMbd hidenHudFromView:self.view];

        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            dataAry= result[@"traces"][@"listItems"];
            for (NSDictionary *dic in dataAry) {
                WuLiuModel *model = [[WuLiuModel alloc]initWithDataDic:dic];
                [self.WuliuArray addObject:model];
            }
            
            [self loadmainView];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MyMbd hidenHudFromView:self.view];

    }
     ];


}
-(void)loadmainView{
    
//    UIView *viewback= [[UIView alloc]initWithFrame:CGRectMake(0, 65, ScreenW, 310)];
//    viewback.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:viewback];
    
    //第一个
  OrderNumberView *ordernumberview = [[[NSBundle mainBundle]loadNibNamed:@"OrderNumberView" owner:nil options:nil]lastObject];
    ordernumberview.frame = CGRectMake(0, 60 , ScreenW, OrderNumberViewheight);
    [ordernumberview gavemeModel:self.orderdetailmodel];
    
    [self.view addSubview:ordernumberview];
    //第二个
    orderProductView *orderproductView = [[[NSBundle mainBundle]loadNibNamed:@"orderProductView" owner:nil options:nil]lastObject];
    orderproductView.frame = CGRectMake(0, CGRectGetMaxY(ordernumberview.frame), ScreenW, orderProductVieHeight);
    orderproductView.deleget = self;
    [orderproductView gaveMeMode:self.orderdetailmodel Withindex:1];
    [self.view addSubview:orderproductView];
    //第三个
    stationView *stationView =[[[NSBundle mainBundle]loadNibNamed:@"stationView" owner:nil options:nil]lastObject];
    stationView.frame = CGRectMake(0, CGRectGetMaxY(orderproductView.frame), ScreenW,stationViewHight );
    [stationView gaveMeModel:self.orderdetailmodel];
    [self.view addSubview:stationView];
    
    //第四个
    if (!self.hidenWuliuView) {
        wuLiuView *wuliuview =[[[NSBundle mainBundle]loadNibNamed:@"wuLiuView" owner:nil options:nil]lastObject];
        wuliuview.frame = CGRectMake(0, CGRectGetMaxY(stationView.frame), ScreenW, wuLiuViewhignt);
        wuliuview.deleget = self;
        [wuliuview gaveMeModel:self.orderdetailmodel];
        [wuliuview gavemeWuLiuModel:self.WuliuArray.lastObject];
        [self.view addSubview:wuliuview];
        

    }    

    
}
#pragma mark ---物流界面的代理
-(void)toOtherVc
{
    LogisticsVC *logvc = [[LogisticsVC alloc]init];
    logvc.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
//    logvc.wuliumodel =self.WuliuArray;
//    logvc.model = self.orderdetailmodel;
    logvc.order_id = self.orderdetailmodel.id_;
    [self presentViewController:logvc animated:YES completion:nil];


}
#pragma mark - 跳到物流
-(void)clicklogst{
}
  #pragma mark - nav
-(void)loadNav{
    self.view.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 20)];
    redview.backgroundColor = DRINK_RED_COLOR;
    [self.view addSubview:redview];
    UILabel *redLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenW, 40)];
    redLab.backgroundColor = DRINK_RED_COLOR;
    redLab.text = @"订单详情";
    redLab.textColor = [UIColor whiteColor];
    redLab.textAlignment =  NSTextAlignmentCenter;
    redLab.font = [UIFont boldSystemFontOfSize:19];
    [self.view addSubview:redLab];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20,44, 40)];
    backBtn.adjustsImageWhenHighlighted = NO;
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
}
-(void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark---productdeleget
//确认收货
-(void)masureGetProduct:(OrderDetailModel *)model
{
    [BackView addToview:self.view WithDeleget:self WithMessage:@"是否确认收货?" WithBtnCount:2];
    self.orderdetailmodel =model;
    
}
//申请退货
-(void)applyBackProduct:(OrderDetailModel *)model
{
    ReturnGoodsVC *vc = [[ReturnGoodsVC alloc]init];
    vc.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
//    vc.block=^()
//    {
//        if ([self.deleget respondsToSelector:@selector(shuaxin)]) {
//            [self.deleget shuaxin];
//        }
//        [self dismissViewControllerAnimated:YES completion:nil];
////        [self loaddata:_state andPage:@"1" withClic:YES];
//        
//    };
//    vc.deleget = self;
    vc.order_id = model.id_;
    vc.productCount = model.count;
    vc.isfromone = NO;
    [self presentViewController:vc animated:YES completion:nil];
    
}
-(void)dismesscontolller
{
    if ([self.deleget respondsToSelector:@selector(shuaxin)]) {
        [self.deleget shuaxin];
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}
//删除订单
-(void)delegetOrder:(OrderDetailModel *)model
{
    [BackView addToview:self.view WithDeleget:self WithMessage:@"是否删除订单?" WithBtnCount:2];
    self.orderdetailmodel = model;
}
//查看原因
-(void)cleckReason:(OrderDetailModel *)model
{
    
    [BackView addToview:self.view WithDeleget:self WithMessage:model.reject_order_reason WithBtnCount:1];
}
//chakanwuliu
-(void)loocForWuliu:(OrderDetailModel *)model
{
    LogisticsVC *vc = [[LogisticsVC alloc]init];
    vc.order_id = model.id_;
    [self presentViewController:vc animated:YES completion:nil];
    
}
//#pragma mark--returnGoodVCdeleget
//-(void)dismessVc
//{
//    if ([self.deleget respondsToSelector:@selector(shuaxin)]) {
//        [self.deleget shuaxin];
//    }
//    [self dismissViewControllerAnimated:YES completion:nil];
//
//}
#pragma mark----网络请求
#pragma mark- 根据订单id删除订单
-(void)deleteOrderWith:(NSString *)orderId{
    [MyMbd addHudToView:self.view];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:orderId forKey:@"ids"];
    [WXDataService requestWithURL:@"mobile/order/deleteOrder.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        [MyMbd hidenHudFromView:self.view];
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            if ([self.deleget respondsToSelector:@selector(shuaxin)]) {
                [self.deleget shuaxin];
            }
            [self dismissViewControllerAnimated:YES completion:nil];
//            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [alertView show];
//            if (self.dataAry.count > 0){
//                [self.dataAry removeObjectAtIndex:deleBtnPath];
//            }
//            [maintableview reloadData];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
#pragma mark- 根据订单id取消订单
-(void)cancelOrderWith:(NSString *)orderId{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:orderId forKey:@"order_id_"];
    [WXDataService requestWithURL:@"mobile/agency/cancelOrder.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            [BackView addToview:self.view WithDeleget:self WithMessage:@"订单取消失败" WithBtnCount:1];
            
            return;
        }else if(suc == 1){
            [BackView addToview:self.view WithDeleget:self WithMessage:@"订单取消成功" WithBtnCount:1];
//            [self.dataAry removeObjectAtIndex:self.ormodel.tag];
//            [maintableview reloadData];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

#pragma mark- 根据id确认收货
-(void)confirmWithId:(NSString *)ordrid{
    //发送确认收货的请求
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:ordrid forKey:@"oreder_id_"];
    [params setObject:@"14" forKey:@"state"];
    [WXDataService requestWithURL:@"mobile/agency/updateOrderState.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        BOOL suc =[success boolValue];
        if (suc == 0 ) {
            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [deleAlertView show];
            return;
        }else if(suc == 1){
            [BackView addToview:self.view WithDeleget:self WithMessage:@"已确认收货" WithBtnCount:1];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误原因:%@",error);
    }];
}
#pragma mark--- backviewDeleget
-(void)makeSureButtenClic:(UIButton *)btn withMessage:(NSString *)message
{
    if ([message isEqualToString:@"是否删除订单?"]) {
        if (btn.tag==0) {
            [self deleteOrderWith:self.orderdetailmodel.id_];
        }
    }else if ([message isEqualToString:@"是否确认收货?"])
    {
        if (btn.tag ==0) {
            [self confirmWithId:self.orderdetailmodel.id_];
        }
    }else if ([message isEqualToString:@"是否取消订单?"])
    {
        if (btn.tag==0) {
            [self cancelOrderWith:self.orderdetailmodel.id_];
        }
    }else if ([message isEqualToString:@"已确认收货"])
    {
        if (btn.tag==0) {
            if ([self.deleget respondsToSelector:@selector(shuaxin)]) {
                [self.deleget shuaxin];
            }
            [self dismissViewControllerAnimated:YES completion:nil];
            
//            [self loaddata:_state andPage:@"1"withClic:YES];
        }
    }else if ([message isEqualToString:@"订单取消成功"])
    {
        if ([self.deleget respondsToSelector:@selector(shuaxin)]) {
            [self.deleget shuaxin];
        }
        [self dismissViewControllerAnimated:YES completion:nil];

    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
